﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLToolkit.Data;
using BLToolkit.Data.Linq;
using BLToolkit.DataAccess;
using BLToolkit.EditableObjects;
using BLToolkit.Mapping;
using BLToolkit;

namespace DrugStore.Library
{
    public static class DBFactory
    {

        public static T Insert<T>(T value, DbManager db)
        {
            try
            {
                System.Data.ConnectionState state =  db.Connection.State;
                var id = db.InsertWithIdentity(value);
                return value;
            }
                catch (Exception e)
            {
                //Logger.Error("Function Insert<" + typeof(T).ToString() + "> in Trans FAILED!!! ---> " + e);
                throw e;
            }
        }

        public static T GetItemByID<T>(int ID, DbManager db)
        {
            try
            {
                SqlQuery<T> qr = new SqlQuery<T>();
                return qr.SelectByKey(ID);
            }
            catch (Exception e)
            {
                //Logger.Error("Function Insert<" + typeof(T).ToString() + "> in Trans FAILED!!! ---> " + e);
                throw e;
            }
        }

        public static List<T> GetListItem<T>(DbManager db)
        {
            try
            {
                SqlQuery<T> qr = new SqlQuery<T>();
                return qr.SelectAll().ToList();
            }
            catch (Exception e)
            {
                //Logger.Error("Function Insert<" + typeof(T).ToString() + "> in Trans FAILED!!! ---> " + e);
                throw e;
            }
        }
        
        public static bool DeleteItem<T>(T value, DbManager db) 
        {
            try
            {
                SqlQuery query = new SqlQuery();
                query.Delete(value);
                return true;
            }
            catch (Exception e)
            {
                //Logger.Error("Function Insert<" + typeof(T).ToString() + "> in Trans FAILED!!! ---> " + e);
                throw e;
            }
        }

        public static T Update<T>(T value, DbManager db)
        {
            try
            {
                SqlQuery query = new SqlQuery(db);
                query.Update(db, value);
                return value;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
